package com.myapp.youba.util;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.myapp.youba.basedata.Microblog;

public class SqliteUtil {
	public static void addMicroblog(Context context, List<Microblog> microblogs) {
		SQLiteDatabase db = SqliteHelper.getDB(context);

		for (int i = 0; i < microblogs.size(); i++) {
			Microblog microblog = microblogs.get(i);
			String sql = "insert into microblog(timestamp,name,email,microblogText) "
					+ "values('"
					+ microblog.getTimestamp().toString()
					+ "','"
					+ microblog.getName()
					+ "','"
					+ microblog.getEmail()
					+ "','" + microblog.getMicroblogText() + "')";
			db.execSQL(sql);
		}

		db.close();

	}

	public static List<Microblog> getMicroblog(Context context) {
		SQLiteDatabase db = SqliteHelper.getDB(context);
		List<Microblog> microblogs = new ArrayList<Microblog>();

		String sql = "select timestamp,name,email,microblogText from microblog";

		Cursor cursor = db.rawQuery(sql, null);

		while (cursor.moveToNext()) {
			Microblog microblog = new Microblog();
			Timestamp timestamp = new Timestamp((cursor.getLong(cursor
					.getColumnIndex("timestamp"))));
			microblog.setTimestamp(timestamp);
			microblog.setName(cursor.getString(cursor.getColumnIndex("name")));
			microblog
					.setEmail(cursor.getString(cursor.getColumnIndex("email")));
			microblog.setMicroblogText(cursor.getString(cursor
					.getColumnIndex("microblogText")));
			microblogs.add(microblog);
		}

		db.close();
		return microblogs;
	}
}
